package com.aerotechnic.entities;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToOne;
import javax.persistence.OneToMany;;

@Entity
public class PlanningMere extends BaseEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@OneToMany(fetch = FetchType.EAGER, targetEntity = Jalon.class)
	protected Set<Jalon> jalon = new HashSet<Jalon>(0);
	@OneToMany(fetch = FetchType.EAGER, targetEntity = MacroTache.class)
	protected Set<MacroTache> macroTache = new HashSet<MacroTache>(0);    
	@OneToMany(fetch = FetchType.EAGER, targetEntity = Link.class)
	protected Set<Link> links = new HashSet<Link>(0);


	public Set<Jalon> getJalon() {
		return jalon;
	}

	public void setJalon(Set<Jalon> jalon) {
		this.jalon = jalon;
	}

	public Set<MacroTache> getMacroTache() {
		return macroTache;
	}

	public void setMacroTache(Set<MacroTache> macroTache) {
		this.macroTache = macroTache;
	}

	public Set<Link> getLinks() {
		return links;
	}

	public void setLinks(Set<Link> links) {
		this.links = links;
	}

	// ************** helper methods ************************
	
	
	
	public void addLink(Link link) {
		links.add(link);
	}
	public void addJalon(Jalon jalon){
		this.jalon.add(jalon);
	}

	public void removeLink(Link link) {
		links.remove(link);
	}

	public void addMacroTache(MacroTache mac) {
		macroTache.add(mac);
	}

	public void removeMacroTache(MacroTache mac) {
		macroTache.remove(mac);
	}

	public void removeMacroTache(Long id) {
		CopyOnWriteArrayList<MacroTache> macHelper = new CopyOnWriteArrayList<MacroTache>(macroTache); 
		for (Iterator<MacroTache> it = macHelper.iterator(); it.hasNext();) {
			MacroTache myMacroTache = it.next();

			if (myMacroTache.getId().equals(id)) {
				macHelper.remove(myMacroTache);
			}
		}   
		macroTache=new HashSet<MacroTache>(macHelper); 
	}
	public void removeLink(Long id){
		CopyOnWriteArrayList<Link> linkHelper = new CopyOnWriteArrayList<Link>(links);
		for(Iterator<Link> it = linkHelper.iterator();it.hasNext();){
			Link myLink = it.next();
			if(myLink.getId().equals(id)){
				linkHelper.remove(myLink);
			}
		}
		links = new HashSet<Link>(linkHelper);
	}
	public void removeJalon(Long id) {
		CopyOnWriteArrayList<Jalon> jalonHelper = new CopyOnWriteArrayList<Jalon>(jalon);
		for (Iterator<Jalon> it = jalonHelper.iterator(); it.hasNext();) {
			Jalon myJalon = it.next();
			if (myJalon.getId().equals(id)) {
				jalonHelper.remove(myJalon);
			}
		}
		jalon = new HashSet<Jalon>(jalonHelper); 
	}

}
